var model_img_url="/images/";
////网络请求框架接入
let RequestManager = require('requestsMode.js');

let sessionManager = require('session.js');
///购物车数量请求成功并且结束
var refreshDone = false;
/*错误吐司--需要错误提示*/
function showErrorToast(errorInfo)
{
    hideLoaddingToast();
    wx.showToast({
        title: errorInfo,
        image : model_img_url + "toastError.png"
    });
}
/*出现成功提示*/
function showSuccessInfoToast(successInfo) {
    hideLoaddingToast();
    wx.showToast({
            title: successInfo,
            icon : "sucess"
        }
    );
}
/*网络错误提示*/
function showWrongNetWorkToast() {
    showErrorToast("网络错误,请稍后重试");
}
/*展示加载框---需要加载提示文字*/
function showLoaddingToast(loaddingInfo) {
    wx.showLoading({
        title: loaddingInfo,
        mask: true
    });
}
/*展示默认加载框*/
function  showLoaddingDefault() {
    showLoaddingToast("加载中...");
}
///获取购物车数量
function requestBuyCarCount(isNetWork,that,currentIndex) {
    if (!isNetWork && refreshDone)
    {
        ///不需要网络请求直接刷新
      reloadTabWithNonNet(that,currentIndex);
    }else
    {
        ///网络请求之后刷新
        RequestManager.sendRequest("member/shop-cart-number","POST",{},{sKey:sessionManager.get("sKey")}).then(
            function (requestSucessRes) {
                if (requestSucessRes.data.code == 200)
                {
                    ///请求成功
                    refreshDone = true;
                    var carCount = requestSucessRes.data.data;
                    getApp().buyCarCount = carCount;
                    reloadTabWithNonNet(that,currentIndex);
                }else
                {
                    ///请求失败
                    refreshDone = false;
                    reloadTabWithNonNet(that,currentIndex);
                }
            },function (requestWithNetWrong)
            {
                refreshDone = false;
                reloadTabWithNonNet(that,currentIndex);
                ///网络错误
            }
        );
    }
}
///刷新tab标签不需要网络请求
function reloadTabWithNonNet(that,currentIndex) {
    ///如果不是有tabBar的界面执行该方法直接跳过  避免crash   刚开始的五个界面进行操作
    if (that.__route__ == 'pages/index/index' || that.__route__ == 'pages/type/type'
        || that.__route__ == 'pages/car/car' || that.__route__ == 'pages/list/list'
        || that.__route__ == 'pages/mines/mines')
    {
        that.setData({
            jhDataForTabbar : tabBarItems(currentIndex,getApp().buyCarCount)
        });
    }
}
/*隐藏加载吐司--停止刷新动画*/
function hideLoaddingToast() {
    wx.hideLoading();
    wx.stopPullDownRefresh();
}
/*tabBar数组*/
function tabBarItems(selectIndex,shopCarCount) {
    ///如果直接赋值为0而且请求下来保存的值不是0则用请求下来的值代替
    if (shopCarCount == 0 && getApp().buyCarCount != 0)
    {
        shopCarCount = getApp().buyCarCount;
    }
    var tempTabItems = [
        {
            active : false, //是否访问当前页面
            url:'index',       //点击访问的url
            sIconUrl: selectIndex == 0 ? model_img_url+"index_active.png" : model_img_url+"index_notactive.png",
            sTitle: "首页"    //按钮名称
        },
        {
            active : false,
            url:'type',
            sIconUrl: selectIndex == 1 ? model_img_url+"type_active.png" : model_img_url+"type_notactive.png",
            sTitle: "分类"
        },
        {
            iCount:shopCarCount,   //购物车订单量
            active : false,
            url:'car',
            sIconUrl: selectIndex == 2 ? model_img_url+"shopCar_active.png" : model_img_url+"shopCar_notactive.png",
            sTitle: "购物车"
        },
        {
            active : false,
            url:'list',
            sIconUrl: selectIndex == 3 ? model_img_url+"list_active.png" : model_img_url+"list_notactive.png",
            sTitle: "常购清单"
        },
        {
            active : false,
            url:'mines',
            sIconUrl: selectIndex == 4 ? model_img_url+"my_active.png" : model_img_url+"my_notactive.png",
            sTitle: "我的"
        }
    ];
    tempTabItems[selectIndex].active = true;
    return tempTabItems;
}
///暴露接口
module.exports.showErrorToast = showErrorToast;
module.exports.showWrongNetWorkToast = showWrongNetWorkToast;
module.exports.showLoaddingToast = showLoaddingToast;
module.exports.showLoaddingDefault = showLoaddingDefault;
module.exports.hideLoaddingToast = hideLoaddingToast;
module.exports.showSuccessInfoToast = showSuccessInfoToast;
module.exports.tabBarItems = tabBarItems;
module.exports.requestBuyCarCount = requestBuyCarCount;